﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Data.GameData.Interfaces;
using Data.GameData;

namespace GUIComponents
{
    public partial class EmpireEconomyToolbox : DockableToolWindow
    {
        public EmpireEconomyToolbox()
        {
            InitializeComponent();
            PanelType = GamePanelType.pECONOMY;
        }

        private void BuildToolbox()
        {
            availableMcLabel.Text = Race.ActiveRace.MonthlyMcTotal.ToString() + " Mc";
            maintCostsLabel.Text = Race.ActiveRace.MaintenanceCosts.ToString() + " Mc";
            totalSpentLabel.Text = Race.ActiveRace.MonthlySpend().ToString() + " Mc";

            turnCompleteCheckBox.CheckState = Race.ActiveRace.TurnComplete ? CheckState.Checked : CheckState.Unchecked;

            foreach (Order order in Race.ActiveRace.Orders)
                AddRow(order);
        
            ordersGrid.SetReadOnly();
        }

        private void UpdateToolbox(BasicEventArgs e)
        {
            totalSpentLabel.Text = Race.ActiveRace.MonthlySpend().ToString() + " Mc";
            turnCompleteCheckBox.CheckState = Race.ActiveRace.TurnComplete ? CheckState.Checked : CheckState.Unchecked;

            if (ordersGrid.Rows.Count() > 1)
                ordersGrid.Rows.RemoveRange(1, ordersGrid.Rows.Count() - 1);

            foreach (Order order in Race.ActiveRace.Orders)
                AddRow(order);

            ordersGrid.SetReadOnly();
        }

        private void EmpireEconomyToolbox_Load(object sender, EventArgs e)
        {
            // Setup order table
            ordersGrid.ColumnsCount = 3;
            ordersGrid.FixedRows = 1;
            ordersGrid.Rows.Insert(0);

            ordersGrid["columnDesc"] = SourceGrid.Cells.Editors.Factory.Create(typeof(string));
            ordersGrid[0, 0] = new SourceGrid.Cells.ColumnHeader("Order");

            ordersGrid["columnCost"] = SourceGrid.Cells.Editors.Factory.Create(typeof(double));
            ordersGrid[0, 1] = new SourceGrid.Cells.ColumnHeader("Cost");

            ordersGrid["columnPaid"] = SourceGrid.Cells.Editors.Factory.Create(typeof(string));
            ordersGrid[0, 2] = new SourceGrid.Cells.ColumnHeader("Paid");

            BuildToolbox();

            ordersGrid.AutoSizeCells();
        }

        private void AddRow(Order order)
        {
            int row = ordersGrid.RowsCount;
            ordersGrid.Rows.Insert(row);

            ordersGrid[row, 0] = new SourceGrid.Cells.Cell(order.Description, ordersGrid["columnDesc"]);
            ordersGrid[row, 1] = new SourceGrid.Cells.Cell(order.Cost, ordersGrid["columnCost"]);
            ordersGrid[row, 2] = new SourceGrid.Cells.Cell(order.TurnCreated == Game.ActiveGame.TurnNumber ? "No" : "Yes", ordersGrid["columnPaid"]);
        }

        private void turnCompleteCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Race.ActiveRace.TurnComplete = turnCompleteCheckBox.CheckState == CheckState.Checked;
        }






        private void deleteOrderToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        public override void UpdatePanel(BasicEventArgs e)
        {
            if (e.EventType == BasicEventType.eGAME)
            {
                BuildToolbox();
            }
            else if (e.EventType == BasicEventType.eORDER)
            {
                if (e.Operation == BasicOperationType.eMODIFY)
                    UpdateToolbox(e);
                else
                    BuildToolbox();
            }
        }
    }
}
